package com.aierjun.ani.utils.getfiledata;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import com.aierjun.ani.utils.SaveImageToGalleryUtils;


/**
 * Created by Administrator on 2017/6/26 0026.
 */

public class GetFileDataUtils {

    /*
    *打开图片相机
    * */
    public static void openImageCream(Activity context, int code){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        context.startActivityForResult(intent, code);
    }

    /*
    * 开发摄像机录制视频
    * */
    public static void openVideoCream(Activity context, int code){
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, code);
        context.startActivityForResult(intent, code);
    }

    /*
    *开打图库
    * */
    public static void openPhoto(Activity context, int code){
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
        context.startActivityForResult(intent, code);
    }

    /*
    * 开发文件管理器
    * */
    public static void openFile(Activity context, int code){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        context.startActivityForResult(intent, code);
    }

    /*
    *获取照相机拍照图片返回
    * */
    public static String getImageCreamPath(Context context, Intent data){
        if (data == null || data.getExtras() == null) return null;
        Bundle bundle = data.getExtras();
        Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式
        String s = SaveImageToGalleryUtils.saveImage(context, bitmap);//存储
        return s;
    }

    /*
    * 获取摄像头拍摄视频返回
    * */
    public static String getVideoCreamPath(Context context, Intent data){
        if (null != data) {
            Uri uri = data.getData();
            if (uri == null) {
                return null;
            } else {
                Cursor c = context.getContentResolver().query(uri,
                        new String[] { MediaStore.MediaColumns.DATA }, null,
                        null, null);
                if (c != null && c.moveToFirst()) {
                    String filPath = c.getString(0);
                    return filPath;
                }
            }
        }
        return null;
    }

    /*
    * 获取选择文件返回
    * */
    public static String getFilePath(Context context, Intent data){
       return UriAllUriUtils.getPath(context,  data.getData());
    }
}
